﻿using LiteDB;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GPDWin4GameBarPowerBackend.AIO.ServiceMain.Models
{

    public class DB : IDisposable
    {
        private LiteDatabase db;

        public DB()
        {
            db = new LiteDatabase(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data.ltdb"));
        }

        public ILiteCollection<QuickPowerButtons> GetQuickPowerButtons()
        {
            return db.GetCollection<QuickPowerButtons>(proFileId + "_" + typeof(QuickPowerButtons).Name);
        }  

        public ILiteCollection<AutoPowerScene> GetAutoPowerScenes()
        {
            return db.GetCollection<AutoPowerScene>(proFileId+"_"+typeof(AutoPowerScene).Name);
        }

        private string proFileId = "default";

        public AppSettings LoadAppSettings() { 
            var col = db.GetCollection<AppSettings>();
            var one = col.Query().Where(f => f._id == proFileId).FirstOrDefault();
            if (one == null)
            {
                initializeDatabase();
                return new AppSettings() { _id = proFileId };
            }
            return one;
        }

        private void initializeDatabase()
        {
            var col = GetQuickPowerButtons();
            col.Insert(new QuickPowerButtons()
            {
                _id = ObjectId.NewObjectId(),
                Name = "默认1",
                Tdp = 28,
                Gpu = 1800,
                Priority = 0
            });
            col.Insert(new QuickPowerButtons()
            {
                _id = ObjectId.NewObjectId(),
                Name = "默认2",
                Tdp = 18,
                Gpu = 1600,
                Priority = 0
            });
            col.Insert(new QuickPowerButtons()
            {
                _id = ObjectId.NewObjectId(),
                Name = "默认3",
                Tdp = 12,
                Gpu = 1200,
                Priority = 0

            });
            col.Insert(new QuickPowerButtons()
            {
                _id = ObjectId.NewObjectId(),
                Name = "默认4",
                Tdp = 6,
                Gpu = 600,
                Priority = 0
            });
        }
        public void SaveAppSettings(AppSettings appSettings)
        {
            var col = db.GetCollection<AppSettings>();
            col.Upsert(appSettings);
        }

        public void Dispose()
        {
            db?.Dispose();
        }
    }

    public class ObjectIdConverter : JsonConverter<ObjectId>
    {
        public override ObjectId ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, ObjectId existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
        {
            var str = reader.Value.ToString();
            return new ObjectId(str);
        }

        public override void WriteJson(Newtonsoft.Json.JsonWriter writer, ObjectId value, Newtonsoft.Json.JsonSerializer serializer)
        {
            writer.WriteValue(value.ToString());
        }
    }
    public class QuickPowerButtons
    {
        public ObjectId _id { get; set; }

        public string Name { get; set; }


        public int Tdp { get; set; } = 18;
        public int Gpu { get; set; } = 1600;

        public int Priority { get; set; } = 10;
    }

    public class AutoPowerScene
    {
        public ObjectId _id { get; set; }
        public string DisplayName { get; set; }
        public string ProcessPath { get; set; }
        /// <summary>
        /// 匹配模式。0=完全匹配，1=前缀匹配，2=仅匹配进程名称
        /// </summary>
        public int MatchType { get; set; } = 0;
        public int Tdp { get; set; } = 18;
        public int Gpu { get; set; } = 1800;
        public int Priority { get; set; } = 10;
    }

    public class AppSettings
    {
        public string _id { get; set; } = "default";
        public int currentTdp { get; set; } = 18;
        public int currentGpu { get; set; } = 1800;

        public int scenarioDefaultTdp { get; set; } = 18;
        public int scenarioDefaultGpu { get; set; } = 1800;
        public bool useBadCoolingConditionDetect { get; set; } = false;
        public bool useScenarioPowerControl { get; set; } = true;

    }
}
